package com.jc.demo.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

public class DemoServlet extends HttpServlet {
	private static final Logger logger = LoggerFactory.getLogger(DemoServlet.class);

	public void init() throws ServletException {
		logger.debug("init demoServlet");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 // response.setContentType("text/html");  
        // 设置字符编码为UTF-8, 这样支持汉字显示  
        // response.setCharacterEncoding("UTF-8");  
  
        response.setContentType("text/json;charset=utf-8");  
  
        /** 设置响应头允许ajax跨域访问 **/  
        response.setHeader("Access-Control-Allow-Origin", "*");  
        /* 星号表示所有的异域请求都可以接受， */  
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");  
        Writer out = response.getWriter();
        out.write("{\"msg\": \"testsuccess\"}");  
        out.flush();  
	}
}
